package com.by.service.impl;

import com.by.bean.TaoCan;
import com.by.mapper.TaoCanMapper;
import com.by.service.TaoCanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaoCanServiceImpl implements TaoCanService {

    @Autowired
    TaoCanMapper taoCanMapper;

    @Transactional
    @Override
    public int save(List<TaoCan> taoCanList) {
        //直接删除 套餐中id 不在给定的集合中
        taoCanMapper.batchDelete(taoCanList);
        taoCanList.forEach(tc -> {
            //判断是不是旧数据
            if (tc.getId() > 0) {
                //修改
                taoCanMapper.update(tc);
            } else {
                //新增
                taoCanMapper.save(tc);
            }
        });

        return 1;
    }

    @Override
    public List<TaoCan> query() {
        return taoCanMapper.query();
    }
}
